package com.android.questions.util;

import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

import com.android.questions.R;

/**
 * FileName: AnimUtil
 * Author: admin
 * Date: 2021/11/22 13:20
 * Description:
 */
public class AnimUtil {

    /**
     * 呼吸放大缩小动画
     *
     * @param context c
     * @param view    v
     */
    public static void receiveAnimM(Context context, View view) {
        Animation animation =
                AnimationUtils.loadAnimation(context, R.anim.anim_dialog_receive_m);
        // 加了AnimationListener，无法用clearAnimation停止动画
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.startAnimation(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(animation);
    }

    /**
     * 呼吸放大缩小动画
     *
     * @param context c
     * @param view    v
     */
    public static void receiveAnim(Context context, ImageView view) {
        Animation animation =
                AnimationUtils.loadAnimation(context, R.anim.anim_dialog_receive);
        // 加了AnimationListener，无法用clearAnimation停止动画
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                view.startAnimation(animation);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        view.startAnimation(animation);
    }

    /**
     * 旋转动画
     *
     * @param imageView iv
     */
    public static void rotateAnim(ImageView imageView) {
        RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin);
        rotate.setDuration(2000);//设置动画持续周期
        rotate.setRepeatCount(Animation.INFINITE);//设置重复次数
        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        rotate.setStartOffset(10);//执行前的等待时间
        imageView.setAnimation(rotate);
    }

    /**
     * 无限循环缩放动画
     *
     * @param imageView 要动的控件
     * @param start     起始大小
     * @param end       结束大小
     */
    public static void scaleAnim(ImageView imageView, float start, float end) {
        ScaleAnimation scaleAnimation = new ScaleAnimation(start, end, start, end, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(800);
        scaleAnimation.setRepeatMode(Animation.REVERSE);
        scaleAnimation.setRepeatCount(Animation.INFINITE);
        scaleAnimation.setInterpolator(new LinearInterpolator());
        scaleAnimation.setFillAfter(false);
        scaleAnimation.start();
        imageView.setAnimation(scaleAnimation);
    }

    /**
     * 气泡上下浮动动画
     *
     * @param context c
     * @param view    v
     */
    public static void upDownAnim(Context context, View view) {
        Animation shakeAnim = AnimationUtils.loadAnimation(context, R.anim.anim_up_down);
        view.startAnimation(shakeAnim);
        shakeAnim.setAnimationListener(null);
    }
}
